package com.zhangyuji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangyuji.entity.AddressBook;
import com.zhangyuji.entity.User;
import com.zhangyuji.mapper.AddressBookMapper;
import com.zhangyuji.service.AddressBookService;
import com.zhangyuji.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Collection;


@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    @Resource
    private UserService userService;


    @Override
    public List<AddressBook> getListByUserAndAddressBook(String phone) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(phone != null,User::getPhone,phone);
        User user = userService.getOne(queryWrapper);
        if (user != null){
            Long userId = user.getId();
            LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(AddressBook::getUserId,userId);
            List<AddressBook> addressBookList = this.list(wrapper);
            return addressBookList;
        }
        return null;
    }

    @Override
    public Long getUserId(String phone) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(phone != null,User::getPhone,phone);
        User user = userService.getOne(queryWrapper);
        return user.getId();
    }
}
